<%@ page import="com.tlb.ejb.*,com.tlb.web.*,com.tlb.dto.*,java.util.*" %>
<%
Util util = Util.getUtil();
String action = request.getParameter("action");
if(action == null || action.trim().equals("")) {
    return;
}
if(action.equals("regCheckUserName")) {
    String name = request.getParameter("name");
    if(name == null || !name.trim().matches("[a-zA-Z0-9]{3,12}")) {
        response.setContentType("text/plain");
        response.getWriter().write("3-12 alphanumeric characters");
        response.getWriter().flush();
        return;
    } else if(new PosterDAO().getPoster(name) != null) {
        response.setContentType("text/plain");
        response.getWriter().write("username taken");
        response.getWriter().flush();
        return;
    } else {
        response.setContentType("text/plain");
        response.getWriter().write("1");
        response.getWriter().flush();
        return;
    }
} else if(action.equals("regCheckPassword")) {
    String name = request.getParameter("name");
    if(name == null || !name.trim().matches("[a-zA-Z0-9]{3,12}")) {
        response.setContentType("text/plain");
        response.getWriter().write("3-12 alphanumeric characters");
        response.getWriter().flush();
        return;
    } else {
        response.setContentType("text/plain");
        response.getWriter().write("1");
        response.getWriter().flush();
        return;
    }
} else if(action.equals("regCheckMoniker")) {
    String name = request.getParameter("name");
    if(name == null || !name.trim().matches(".{1,45}")) {
        response.setContentType("text/plain");
        response.getWriter().write("1-45 characters");
        response.getWriter().flush();
        return;
    } else if(new MonikerDAO().getMoniker(name) != null) {
        response.setContentType("text/plain");
        response.getWriter().write("moniker taken");
        response.getWriter().flush();
        return;
    } else {
        response.setContentType("text/plain");
        response.getWriter().write("1");
        response.getWriter().flush();
        return;
    }
    


          
} else if(action.equals("flagPost")) {
    int postId = -1;
    int threadId = -1;
    int flagId = -1;
    try {
        flagId = Integer.parseInt(request.getParameter("flagId"));
        postId = Integer.parseInt(request.getParameter("postId"));
        threadId = Integer.parseInt(request.getParameter("threadId"));
    } catch(Exception e) {
        System.out.println("Error writing postFlag postId: " + postId + " / flagId: " + flagId + " / threadId: " + threadId);
        return;
    }
    
    Poster p = new AuthManager(request, response).softAuthenticatePoster();
    if(p != null) { //no point in logging flag even if you aren't logged in right now.
        if(p.getUserLevelId() == 1 && flagId != 1 && flagId != 2) { //admin & not bestof or helpful
            PostDAO pDao = new PostDAO();
            Post updPost = null;
            if(flagId == 10) { //sticky - thread level
                new ThreadFacade(p.getPosterId(), threadId).toggleSticky();
            } else {
                updPost = pDao.getPostById(flagId >= 11 ? threadId : postId); //>=11 = thread level, under = post level
                if(flagId == 11 || flagId == 5) { //TOS
                    updPost.setTos(!updPost.getTos());
                } else if(flagId == 12 || flagId == 4) { //NSFW
                    updPost.setNsfw(!updPost.getNsfw());
                } else if(flagId == 13 || flagId == 3) { //SPAM
                    updPost.setSpam(!updPost.getSpam());
                }
                if(updPost != null)
                    pDao.updatePost(updPost);
            }                         
        } else { //nonprivileged poster, or bestof or helpful
        
            PostFlag pf = new PostFlag();
            pf.setFlagId(flagId);
            pf.setPostId(postId);
            pf.setPosterId(p.getPosterId());

            ThreadDAO tdao = new ThreadDAO();
            tdao.savePostFlag(pf);
        }    	
    }

    response.setContentType("text/plain");
    response.getWriter().print(postId);
    response.getWriter().flush();
    
    return;
    
    
    
    
    
    
} else if(action.equals("chgPassword")) {
	Poster p = new AuthManager(request, response).softAuthenticatePoster();
	if(p == null) {
		return;
	}

	String p1 = request.getParameter("password1");
    String p2 = request.getParameter("password2");
    
    if(p1 == null || p2 == null || !p1.equals(p2) || !p1.trim().matches("[a-zA-Z0-9]{3,12}")) {
        response.setContentType("text/plain");
        response.getWriter().write("3-12 alphanumeric characters");
        response.getWriter().flush();
        return;
    } else {
        new PosterDAO().updatePassword(p.getPosterId(), p1);

        response.setContentType("text/plain");
        response.getWriter().write("1");
        response.getWriter().flush();
        return;
    }

    
    
    
    
    
    
    
} else if(action.equals("chgFilter")) {
	Poster p = new AuthManager(request, response).softAuthenticatePoster();
	if(p == null) {
		return;
	}
    
    String spam = request.getParameter("spam");
    String offense = request.getParameter("offense");
    
    if(spam == null || offense == null)
        return;
    

    new PosterDAO().updateFilter(p.getPosterId(), spam.equals("true"), offense.equals("true"));
    
    
    response.setContentType("text/plain");
    response.getWriter().write("1");
    response.getWriter().flush();
    return;
    
    
    
    
    
    
    
} else if(action.equals("chgMoniker")) {
	
	
	int monikerId = -1;
	int primMon = -1;
	Boolean showCounter = null;
	String tag = null;
	try {
		monikerId = Integer.parseInt(request.getParameter("monikerId"));
		primMon = Integer.parseInt(request.getParameter("primMon"));
		showCounter = new Boolean(request.getParameter("showCounter"));
		tag = request.getParameter("tag");
	} catch(Exception e) {
        System.out.println("Error chgMoniker monikerId: " + request.getParameter("monikerId") + " / primMon: " + request.getParameter("primMon") + " / showCounter: " + request.getParameter("showCounter") + " / tag: " + request.getParameter("tag"));
        return;
    }
		
	Poster p = new AuthManager(request, response).softAuthenticatePoster(monikerId);
	if(p == null) {
		return;
	}
    
    if(tag == null || primMon < 1 || showCounter == null)
        return;

    Moniker m = new MonikerDAO().getMoniker(monikerId);
    
    m.setTagLine(tag);
    m.setShowCounter(showCounter);
    new MonikerDAO().saveMoniker(m, false);
    new PosterDAO().updatePrimaryMonikerId(p, new MonikerDAO().getMoniker(primMon));
        
    response.setContentType("text/plain");
    response.getWriter().write("1," + m.getMonikerId());
    response.getWriter().flush();
    return;
    
    
    
    
    
    
} else if(action.equals("chgReply")) {
	
	
	Poster p = new AuthManager(request, response).softAuthenticatePoster();
	if(p == null) {
		return;
	}
    
    int inlineReply = -1;
    try {
		inlineReply = Integer.parseInt(request.getParameter("inlineReply"));
	} catch(Exception e) {
        System.out.println("Error chgReply inlineReply: " + inlineReply);
        return;
    }    
    
    if(inlineReply < 0 || inlineReply > 1)
        return;

    new PosterDAO().updateInlineReply(p.getPosterId(), inlineReply);
    
    
    response.setContentType("text/plain");
    response.getWriter().write("1");
    response.getWriter().flush();
    return;
    
    
    
} else if(action.equals("addMoniker")) {
	Poster p = new AuthManager(request, response).softAuthenticatePoster();
	if(p == null) {
		return;
	}

    
    
    String tag = request.getParameter("tag");
    String name = request.getParameter("name");
    String primMon = request.getParameter("primMon");
    Boolean showCounter = new Boolean(request.getParameter("showCounter"));

    if(name == null || name.trim().equals("") || Integer.parseInt(primMon) < -1 || (tag != null && tag.length() > 45)) {
        return;
    }
    
    
    MonikerDAO mdao = new MonikerDAO();
    Moniker existing = mdao.getMoniker(name);
    if(existing != null) {
        System.out.println("Attempt to create existing moniker: " + name);
        return;        
    }
    
    Moniker newMon = new Moniker();
    newMon.setName(name);
    newMon.setTagLine(tag);
    newMon.setShowCounter(showCounter);
    mdao.saveMoniker(newMon, p.getPosterId());
    
    
    if(primMon.equals("-1")) {
        new PosterDAO().updatePrimaryMonikerId(p, newMon);
    }
    
    response.setContentType("text/plain");
    response.getWriter().write("1");
    response.getWriter().flush();
    return;
}






%>